using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using GonzalesCore.CoreElements;

using Kypris2.Utils;
using Kypris2.CoreElements;
using Kypris2.CoreManagers;

using Jamila2.CoreElements;
using Jamila2.Database;
using Jamila2.Tools;

namespace GonzalesCore.CoreManagers
{
    public class ReportManager : KyprisManagerBase, IManager, IDisposable
    {
        private GonzalesDataStructure DataStructrure = new GonzalesDataStructure();
        private PersonalSessionAccount CurrentModuleSession = null;

        public ReportManager(PersonalSessionAccount PrivateConfig)
            : base(PrivateConfig)
        {
            this.CurrentModuleSession = PrivateConfig;                      
        }

        public DataTable ReportClaimHeader(string CompanyID, DateTime DateSubmittedStart, DateTime DateSubmittedEnd)
        {
            DataTable ResultTable = null;

            DatabaseParameters KeyParameters = new DatabaseParameters();
            KeyParameters.Add(new DatabaseParameter(this.DataStructrure.Views.OpClaimHeaders.Company.ActualFieldName, CompanyID));
            KeyParameters.Add(new DatabaseParameter(this.DataStructrure.Views.OpClaimHeaders.DateSubmitted.ActualFieldName, DateSubmittedStart.ToShortDateString(), DBDataType.Date, DBLinkage.AND, DBCompareType.LargerNEqual));
            KeyParameters.Add(new DatabaseParameter(this.DataStructrure.Views.OpClaimHeaders.DateSubmitted.ActualFieldName, DateSubmittedStart.ToShortDateString(), DBDataType.Date, DBLinkage.AND, DBCompareType.LargerNEqual));

            if (this.TryConnection())
            {
                this.CurSQLFactory.SelectCommand(KeyParameters, this.DataStructrure.Views.OpClaimHeaders.ActualTableName);
                DataTable CurrentResult = this.CurDBEngine.SelectQuery(this.CurSQLFactory.SQL);

                if (CurrentResult != null)
                {
                    ResultTable = new DataTable();

                    ResultTable.Columns.Add(new DataColumn("ClaimID", Type.GetType("System.String")));
                    ResultTable.Columns.Add(new DataColumn("Category", Type.GetType("System.String")));
                    ResultTable.Columns.Add(new DataColumn("Reason", Type.GetType("System.String")));
                    ResultTable.Columns.Add(new DataColumn("Remarks", Type.GetType("System.String")));
                    ResultTable.Columns.Add(new DataColumn("ClaimedBy", Type.GetType("System.String")));
                    ResultTable.Columns.Add(new DataColumn("Status", Type.GetType("System.String")));
                    ResultTable.Columns.Add(new DataColumn("Currency", Type.GetType("System.String")));
                    ResultTable.Columns.Add(new DataColumn("AmountClaimed", Type.GetType("System.String")));
                    ResultTable.Columns.Add(new DataColumn("FinalAmount", Type.GetType("System.String")));
                    ResultTable.Columns.Add(new DataColumn("LessCardTransactions", Type.GetType("System.String")));
                    ResultTable.Columns.Add(new DataColumn("OwingEmployee", Type.GetType("System.String")));

                    foreach (DataRow CurrentRow in CurrentResult.Rows)
                    {
                        DataRow ResultRow = ResultTable.NewRow();

                        ResultRow[""] = CurrentRow[""];
                        ResultRow[""] = CurrentRow[""];
                        ResultRow[""] = CurrentRow[""];
                        ResultRow[""] = CurrentRow[""];
                        ResultRow[""] = CurrentRow[""];
                        ResultRow[""] = CurrentRow[""];
                        ResultRow[""] = CurrentRow[""];
                        ResultRow[""] = CurrentRow[""];
                        ResultRow[""] = CurrentRow[""];
                        ResultRow[""] = CurrentRow[""];
                        ResultRow[""] = CurrentRow[""];

                    }
                }
                else
                {
                    this.ErrMsg = "[ReportManager.ReportClaimHeader] : Failed at this.CurDBEngine.SelectQuery('" + this.CurSQLFactory.SQL + "') : " + this.CurDBEngine.ErrorMessage;
                }
            }
            else
            {
                this.ErrMsg = "[ReportManager.ReportClaimHeader] : Exception : " + this.ErrMsg;
            }

            return ResultTable;
        }

        public DataTable AccrualReport(string LanguageID, string CompanyID, DateTime DateFrom, DateTime DateTo,string ClaimStatus)
        {
            DataTable ResultTable = null;

            DatabaseParameters KeyParameters = new DatabaseParameters();
            KeyParameters.Add(new DatabaseParameter(this.DataStructrure.StoredProcedures.AcrualReport.Param_LanguageID.ActualFieldName, LanguageID));
            KeyParameters.Add(new DatabaseParameter(this.DataStructrure.StoredProcedures.AcrualReport.Param_CompanyID.ActualFieldName, CompanyID));
            //KeyParameters.Add(new DatabaseParameter(this.DataStructrure.StoredProcedures.AcrualReport.Param_DetailType.ActualFieldName, DetailType));
            KeyParameters.Add(new DatabaseParameter(this.DataStructrure.StoredProcedures.AcrualReport.Param_DateFrom.ActualFieldName, DateFrom.ToString()));
            KeyParameters.Add(new DatabaseParameter(this.DataStructrure.StoredProcedures.AcrualReport.Param_DateTo.ActualFieldName, DateTo.ToString()));
            KeyParameters.Add(new DatabaseParameter(this.DataStructrure.StoredProcedures.AcrualReport.Param_Claimstatus.ActualFieldName, ClaimStatus.ToString()));

            if (this.TryConnection())
            {
                this.CurSQLFactory.ExecuteStoredProcedure(this.DataStructrure.StoredProcedures.AcrualReport.ActualTableName, KeyParameters);
                ResultTable = this.CurDBEngine.SelectQuery(this.CurSQLFactory.SQL);

                if (ResultTable == null)
                {
                    this.ErrMsg = "[ReportManager.AccrualReport] : Failed at this.CurDBEngine.SelectQuery('" + this.CurSQLFactory.SQL + "') : " + this.CurDBEngine.ErrorMessage;
                }
            }
            else
            {
                this.ErrMsg = "[ReportManager.AccrualReport] : Exception : " + this.ErrMsg;
            }

            return ResultTable;
        }

        public DataTable AccrualReportSummary(string LanguageID, string CompanyID, DateTime DateFrom, DateTime DateTo, string ClaimStatus)
        {
            DataTable ResultTable = null;

            DatabaseParameters KeyParameters = new DatabaseParameters();
            KeyParameters.Add(new DatabaseParameter(this.DataStructrure.StoredProcedures.AcrualSummaryReport.Param_LanguageID.ActualFieldName, LanguageID));
            KeyParameters.Add(new DatabaseParameter(this.DataStructrure.StoredProcedures.AcrualSummaryReport.Param_CompanyID.ActualFieldName, CompanyID));
            //KeyParameters.Add(new DatabaseParameter(this.DataStructrure.StoredProcedures.AcrualReport.Param_DetailType.ActualFieldName, DetailType));
            KeyParameters.Add(new DatabaseParameter(this.DataStructrure.StoredProcedures.AcrualSummaryReport.Param_DateFrom.ActualFieldName, DateFrom.ToString()));
            KeyParameters.Add(new DatabaseParameter(this.DataStructrure.StoredProcedures.AcrualSummaryReport.Param_DateTo.ActualFieldName, DateTo.ToString()));
            KeyParameters.Add(new DatabaseParameter(this.DataStructrure.StoredProcedures.AcrualSummaryReport.Param_Claimstatus.ActualFieldName, ClaimStatus.ToString()));

            if (this.TryConnection())
            {
                this.CurSQLFactory.ExecuteStoredProcedure(this.DataStructrure.StoredProcedures.AcrualSummaryReport.ActualTableName, KeyParameters);
                ResultTable = this.CurDBEngine.SelectQuery(this.CurSQLFactory.SQL);

                if (ResultTable == null)
                {
                    this.ErrMsg = "[ReportManager.AccrualReport] : Failed at this.CurDBEngine.SelectQuery('" + this.CurSQLFactory.SQL + "') : " + this.CurDBEngine.ErrorMessage;
                }
            }
            else
            {
                this.ErrMsg = "[ReportManager.AccrualReport] : Exception : " + this.ErrMsg;
            }

            return ResultTable;
        }

        //Added by Agnes[09Dec2008] - Report for china enhancement
        public DataTable ChinaAccrualReport(string CompanyID)
        {
            DataTable ResultTable = null;

            DatabaseParameters KeyParameters = new DatabaseParameters();
            KeyParameters.Add(new DatabaseParameter(this.DataStructrure.StoredProcedures.ChinaAcrualReport.Param_CompanyID.ActualFieldName, CompanyID));
            if (this.TryConnection())
            {
                this.CurSQLFactory.ExecuteStoredProcedure(this.DataStructrure.StoredProcedures.ChinaAcrualReport.ActualTableName, KeyParameters);
                ResultTable = this.CurDBEngine.SelectQuery(this.CurSQLFactory.SQL);

                if (ResultTable == null)
                {
                    this.ErrMsg = "[ReportManager.ChinaAccrualReport] : Failed at this.CurDBEngine.SelectQuery('" + this.CurSQLFactory.SQL + "') : " + this.CurDBEngine.ErrorMessage;
                }
            }
            else
            {
                this.ErrMsg = "[ReportManager.ChinaAccrualReport] : Exception : " + this.ErrMsg;
            }

            return ResultTable;
        }

        public DataTable CreditCardOverviewReport(string CompanyID)
        {
            DataTable ResultTable = null;

            DatabaseParameters KeyParameters = new DatabaseParameters();
            KeyParameters.Add(new DatabaseParameter(this.DataStructrure.Views.CreditCardReport.CompanyInternalID.ActualFieldName, CompanyID));

            if (this.TryConnection())
            {
                this.CurSQLFactory.SelectCommand(KeyParameters, this.DataStructrure.Views.CreditCardReport.ActualTableName);
                ResultTable = this.CurDBEngine.SelectQuery(this.CurSQLFactory.SQL);

                if (ResultTable == null)
                {
                    this.ErrMsg = "[ReportManager.CreditCardOverviewReport] : Failed at this.CurDBEngine.SelectQuery('" + this.CurSQLFactory.SQL + "') : " + this.CurDBEngine.ErrorMessage;
                }
            }
            else
            {
                this.ErrMsg = "[ReportManager.CreditCardOverviewReport] : Exception : " + this.ErrMsg;
            }

            return ResultTable;
        }
    }
}
